//
//  ViewController.m
//  web-app
//
//  Created by 黄晓容 on 16/5/27.
//  Copyright © 2016年 cloud. All rights reserved.
//

#import "ViewController.h"

#import "BridgeHelp.h"

#import "ImageHandleViewController.h"

#import "CameraViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController () <UIWebViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,ImageHandleViewControllerDelegate>

@property (nonatomic,strong) UIWebView *webView;

@property (nonatomic,strong) BridgeHelp *bridgeHelp;

@property (nonatomic,strong) PhotoModel *photoModel;
@property (nonatomic,strong) UIImagePickerController *pickerImage;

@property (nonatomic,strong) UIView *cameraOverlayView;

@property (nonatomic,strong) AVCaptureSession *captureSession;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configParams];
    [self configViews];
    [self configDatas];
}

- (void)configParams
{
    self.view.backgroundColor = [UIColor greenColor];
}

- (void)configViews
{
    [self.view addSubview:self.webView];
}

- (void)configDatas
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *url = [bundle URLForResource:@"web" withExtension:@"html"];
    
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [self.webView loadRequest:request];
}

#pragma mark - 使用懒加载配置视图界面
- (UIWebView *)webView
{
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
        _webView.delegate = self;
    }
    return _webView;
}

- (UIImagePickerController *)pickerImage
{
    if (!_pickerImage) {
        _pickerImage = [[UIImagePickerController alloc] init];
    }
    return _pickerImage;
}

- (UIView *)cameraOverlayView
{
    if (!_cameraOverlayView) {
        _cameraOverlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _cameraOverlayView.backgroundColor = [UIColor redColor];
    }
    return _cameraOverlayView;
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.bridgeHelp = [[BridgeHelp alloc] initWithWebView:webView];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0)
{
    [picker dismissViewControllerAnimated:NO completion:nil];
    if(self.photoModel.photoSource != UIImagePickerControllerSourceTypeCamera)
    {
        [self handleImage:image];
    }
}

- (void)imageDidHandle:(UIImage *)handleImage
{
    [self.bridgeHelp showImage:handleImage];
}

#pragma mark - action
- (void)startCapture:(PhotoModel *)photoModel
{
    self.photoModel = photoModel;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view endEditing:YES];
    });
    
    if (self.photoModel.photoSource == UIImagePickerControllerSourceTypeCamera) {
        [self cameraCaptureHandle];
    }else{
        [self photosCaptureHandle];
    }
}

- (void)cameraCaptureHandle
{
    CameraViewController *cameraVC = [[CameraViewController alloc] initWithPositionDevice:self.photoModel.cameraType];
    __weak CameraViewController *__cameraVC = cameraVC;
    cameraVC.cameraCaptureImage = ^(UIImage *image){
        [__cameraVC dismissViewControllerAnimated:NO completion:nil];
        [self handleImage:image];
    };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:cameraVC animated:NO completion:nil];
    });
}

- (void)photosCaptureHandle
{
    if ([UIImagePickerController isSourceTypeAvailable:self.photoModel.photoSource]) {
        self.pickerImage.sourceType = self.photoModel.photoSource;
    }
    
    self.pickerImage.delegate = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:self.pickerImage animated:YES completion:nil];
    });
}

- (void)handleImage:(UIImage *)image
{
    CGSize cutSize = CGSizeMake(self.photoModel.width, self.photoModel.height);
    
    //假如图片裁剪大小大于实际图片大小，那么显示错误信息
    if(cutSize.width > image.size.width || cutSize.height > image.size.height)
    {
        [self.bridgeHelp showMsg:@"裁剪尺寸不能大于图片尺寸"];
        return;
    }
    
    ImageHandleViewController *imageHandleVC = [[ImageHandleViewController alloc] initWithImage:image andCutSize:cutSize];
    imageHandleVC.imageHandleDelegate = self;
    [self.navigationController pushViewController:imageHandleVC animated:NO];
}

@end
